package com.one.group.admin.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.convert.Convert;
import com.mybatisflex.core.tenant.TenantFactory;
import com.one.group.model.dto.BaseDeleteDto;
import com.one.group.model.dto.InsertNoticeDto;
import com.one.group.model.dto.NoticeQueryDto;
import com.one.group.model.dto.UpdateNoticeDto;
import com.one.group.result.Result;
import com.one.group.service.NoticeService;
import com.one.group.utils.ResultUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author kld
 * @date 2025/9/28 15:42
 * @description TODO
 */
@RequestMapping("/notice")
@RestController
@Tag(name = "公告")
@Slf4j
public class NoticeController {
    @Autowired
    NoticeService noticeService;

    @PostMapping("/add")
    @Operation(summary = "新增公告")
    public Result add(@RequestBody InsertNoticeDto insertNoticeDto){

        log.debug("新增数据：{}", insertNoticeDto);
        noticeService.saveByDto(insertNoticeDto);
        return ResultUtil.success();
    }
    @PostMapping(value = "/deleteByIds")
    @Operation(summary = "批量删除")
    public Result deleteByIds(@RequestBody BaseDeleteDto baseDeleteDto){
        boolean result = noticeService.removeByIds(baseDeleteDto.getIds());
        if (result){
            return ResultUtil.success(baseDeleteDto);
        }
        return  ResultUtil.error(500, "删除失败");
    }

    @PostMapping(value = "/update")
    @Operation(summary = "修改")
    public Result update(@RequestBody UpdateNoticeDto updateNoticeDto){
        noticeService.updateByDto(updateNoticeDto);
        return  ResultUtil.success(noticeService.getById(updateNoticeDto.getId()));
    }
    @GetMapping(value = "/queryById")
    @Operation(summary = "查询")
    public Result queryById(Long id){
        return  ResultUtil.success(noticeService.getById(id));
    }
    //后续租户id查
    @GetMapping(value = "/queryAll")
    @Operation(summary = "查询所有")
    public Result queryAll(){
        return  ResultUtil.success(noticeService.list());
    }


    @GetMapping(value = "/queryByDto")
    @Operation(summary = "模糊查询")
    public Result queryByDto(NoticeQueryDto noticeQueryDto){
        return  ResultUtil.success(noticeService.findByNoticeQueryDto(noticeQueryDto));
    }

    @GetMapping(value = "/publish")
    @Operation(summary = "发布")
    public Result publish(Long id){
        return  ResultUtil.success(noticeService.publish(id));
    }

}
